package net.plentz.tagger.ui.widgets;

import net.plentz.tagger.ui.listeners.events.AbstractEvent;
import net.plentz.tagger.ui.listeners.events.AbstractEvent.Action;
import net.plentz.tagger.ui.listeners.events.GeneralEvent;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class GlobalDirectoryWidget extends AbstractWidget {

	private Text directoryInput;

	@Override
	public Composite render(Composite parent, int flags) {

		final Composite c = new Composite(parent, flags);
		GridData gridData;

		// Layout
		c.setLayout(new GridLayout(3, false));

		// Label
		Label label = this.appendLabel(c);
		label.setLayoutData(new GridData(SWT.NONE, SWT.CENTER, false, true, 1, 1));

		// Directory field
		this.directoryInput = this.appendDirectoryField(c);
		gridData = new GridData(SWT.NONE, SWT.CENTER, false, true, 1, 1);
		gridData.widthHint = 256;
		this.directoryInput.setLayoutData(gridData);

		// Browse button
		Button browseButton = this.appendBrowseButton(c);
		browseButton.setLayoutData(new GridData(SWT.NONE, SWT.CENTER, false, true, 1, 1));

		// Actions
		this.directoryInput.addKeyListener(new KeyListener() {

			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void keyReleased(KeyEvent e) {
				switch (e.keyCode) {
					case SWT.CR:
						Text text = (Text)e.getSource();
						GlobalDirectoryWidget.this.setDirectory(text.getText());
						break;
				}
			}

		});
		browseButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dlg = new DirectoryDialog(c.getShell());
				dlg.setFilterPath(GlobalDirectoryWidget.this.directoryInput.getText()); // Initial
																						// value
				dlg.setText("Select a Directory");
				dlg.setMessage("Select a directory containing any music files you'd like to process.");
				String directory = dlg.open();
				if (directory != null) {
					GlobalDirectoryWidget.this.setDirectory(directory);
				}
			}
		});

		c.pack();
		return c;

	}

	public void setDirectory(String directory) {
		this.directoryInput.setText(directory);
		this.broadcastEvent(new GeneralEvent(Action.SET_GLOBAL_DIRECTORY, directory));
	}

	private Label appendLabel(Composite parent) {

		Label label = new Label(parent, SWT.NONE);
		FontData fontData = label.getFont().getFontData()[0];
		fontData.setStyle(SWT.BOLD);
		label.setFont(new Font(label.getDisplay(), fontData));
		label.setText("Current Directory:");
		return label;

	}

	private Text appendDirectoryField(Composite parent) {

		Text text = new Text(parent, SWT.BORDER);
		return text;

	}

	private Button appendBrowseButton(Composite parent) {

		Button button = new Button(parent, SWT.NONE);
		button.setText("Browse...");
		return button;

	}

	@Override
	protected void invokeEvent(AbstractEvent event) {
		// TODO Auto-generated method stub

	}

}
